package com.yuexunit.mobilenurse.module.BloodManage.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.module.BloodManage.bean.Blood;
import com.yuexunit.mobilenurse.module.BloodManage.bean.BloodBag;
import com.yuexunit.mobilenurse.module.BloodManage.callback.IReceiveBloodCallBack;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.Collection;

/**
 * Created by wangt on 2017/10/23.
 */

public class BloodBagStateAdapter extends KJAdapter<BloodBag> {
    IReceiveBloodCallBack iReceiveBloodCallBack;
    private Blood dataBean;

    public BloodBagStateAdapter(AbsListView view, Blood dataBean, Collection<BloodBag> mDatas, IReceiveBloodCallBack iReceiveBloodCallBack, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
        this.dataBean = dataBean;
        this.iReceiveBloodCallBack = iReceiveBloodCallBack;
    }

    @Override
    public void convert(AdapterHolder helper, final BloodBag item, boolean isScrolling, final int position) {
        super.convert(helper, item, isScrolling, position);
        helper.setText(R.id.tv_bag_num, item.getHisBagNo());
        helper.setText(R.id.tv_blood_kind, item.getBloodKind());
        helper.setText(R.id.tv_blood_amount, item.getBloodNum());

        LinearLayout llSend = helper.getView(R.id.ll_blood_send);
        LinearLayout llReceive = helper.getView(R.id.ll_blood_receive);
        LinearLayout llBefore = helper.getView(R.id.ll_blood_before);
        LinearLayout llBeforeAudit = helper.getView(R.id.ll_before_audit);
        LinearLayout llBegin = helper.getView(R.id.ll_blood_begin);
        LinearLayout llBeginAudit = helper.getView(R.id.ll_begin_audit);
        LinearLayout llBegin_15 = helper.getView(R.id.ll_blood_begin_15);
        LinearLayout llFinish = helper.getView(R.id.ll_blood_finish);
        LinearLayout llFinish_4 = helper.getView(R.id.ll_blood_finish_4);
        LinearLayout llReturn = helper.getView(R.id.ll_blood_return);

        TextView receive_btn = helper.getView(R.id.receive_blood_btn);

        receive_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(iReceiveBloodCallBack != null){
                    iReceiveBloodCallBack.callBack(position,item.getHisBagNo());
                }
            }
        });

        //送血详情
        if (!TextUtils.isEmpty(dataBean.getAttachJobName())) {
            helper.setText(R.id.tv_send_date, dataBean.getAttachTime());
            helper.setText(R.id.tv_send_name, dataBean.getAttachJobName());
            helper.setText(R.id.tv_send_jobnum, dataBean.getAttachJobNum());
            llSend.setVisibility(View.VISIBLE);
        } else {
            llSend.setVisibility(View.GONE);
        }

        //接血详情
        if (!TextUtils.isEmpty(item.getReceiveName())) {
            receive_btn.setVisibility(View.GONE);
            helper.setText(R.id.tv_receive_date, item.getReceiveTime());
            helper.setText(R.id.tv_receive_name, item.getReceiveName());
            helper.setText(R.id.tv_receive_jobnum, item.getReceiveJobNum());
            llReceive.setVisibility(View.VISIBLE);
        } else {
            receive_btn.setVisibility(View.VISIBLE);
            llReceive.setVisibility(View.GONE);
        }

        //用血前核对详情
        if (!TextUtils.isEmpty(item.getBeforeName())) {
            helper.setText(R.id.tv_before_date, item.getBeforeTime());
            helper.setText(R.id.tv_before_name, item.getBeforeName());
            helper.setText(R.id.tv_before_jobnum, item.getBeforeJobNum());
            llBefore.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(item.getBeforeAuditName())) {
                helper.setText(R.id.tv_before_audit_name, item.getBeforeAuditName());
                helper.setText(R.id.tv_before_audit_jobnum, item.getBeforeAuditJobNum());
                llBeforeAudit.setVisibility(View.VISIBLE);
            } else {
                llBeforeAudit.setVisibility(View.GONE);
            }
        } else {
            llBefore.setVisibility(View.GONE);
        }

        //输血开始详情
        if (!TextUtils.isEmpty(item.getBeginName())) {
            helper.setText(R.id.tv_begin_date, item.getBeginTime());
            helper.setText(R.id.tv_begin_name, item.getBeginName());
            helper.setText(R.id.tv_begin_jobnum, item.getBeginJobNum());
            llBegin.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(item.getBeginAuditName())) {
                helper.setText(R.id.tv_begin_audit_name, item.getBeginAuditName());
                helper.setText(R.id.tv_begin_audit_jobnum, item.getBeginAuditJobNum());
                llBeginAudit.setVisibility(View.VISIBLE);
            } else {
                llBeginAudit.setVisibility(View.GONE);
            }
        } else {
            llBegin.setVisibility(View.GONE);
        }

        //输血开始15分钟后
        if (!TextUtils.isEmpty(item.getBeginConfirmName())) {
            helper.setText(R.id.tv_begin_15_date, item.getBeginConfirmTime());
            helper.setText(R.id.tv_begin_15_name, item.getBeginConfirmName());
            helper.setText(R.id.tv_begin_15_jobnum, item.getBeginConfirmJobNum());
            helper.setText(R.id.tv_begin_15_remark, item.getBeginConfirmRemark());
            llBegin_15.setVisibility(View.VISIBLE);
        } else {
            llBegin_15.setVisibility(View.GONE);
        }

        //输血完成
        if (!TextUtils.isEmpty(item.getFinishName())) {
            helper.setText(R.id.tv_finish_date, item.getFinishTime());
            helper.setText(R.id.tv_finish_name, item.getFinishName());
            helper.setText(R.id.tv_finish_jobnum, item.getFinishJobNum());
            helper.setText(R.id.tv_finish_remark, item.getFinishRemark());
            llFinish.setVisibility(View.VISIBLE);
        } else {
            llFinish.setVisibility(View.GONE);
        }

        //输血完成后4小时
        if (!TextUtils.isEmpty(item.getFinishConfirmName())) {
            helper.setText(R.id.tv_finish_4_date, item.getFinishConfirmTime());
            helper.setText(R.id.tv_finish_4_name, item.getFinishConfirmName());
            helper.setText(R.id.tv_finish_4_jobnum, item.getFinishConfirmJobNum());
            helper.setText(R.id.tv_finish_4_remark, item.getFinishConfirmRemark());
            llFinish_4.setVisibility(View.VISIBLE);
        } else {
            llFinish_4.setVisibility(View.GONE);
        }

        //退血详情
        if (item.getCancleStatus().equals("-1")) {
            helper.setText(R.id.tv_return_date, item.getCancleTime());
            helper.setText(R.id.tv_return_name, item.getCancleName());
            helper.setText(R.id.tv_return_jobnum, item.getCancleJobNum());
            llReturn.setVisibility(View.VISIBLE);
        } else {
            llReturn.setVisibility(View.GONE);
        }
    }
}
